package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpServer implements Runnable {

    private ServerSocket welcomeSocket;
    private ExecutorService exec = Executors.newFixedThreadPool( 1000 );
    public static MimeTypeReader mimeReader = new MimeTypeReader();

    public HttpServer( int portnr ) {
	System.out.println( "trying to get port" );
	try {
	    welcomeSocket = new ServerSocket( portnr );
	} catch ( IOException e ) {
	    System.err.println( "Could not listen on port:" + portnr );
	    System.exit( -1 );
	}
	System.err.println( "Now listening on port:" + portnr );
    }

    public void run() {
	while ( true ) {
	    try {
		exec.execute( new HttpServerThread( welcomeSocket.accept() ) );
	    } catch ( IOException e ) {
		e.printStackTrace();
	    }
	}
    }

}
